import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<>();

        for (int i = 1; i < 8; i++) {
            treeSet.add(i);
        }

        Integer first = treeSet.first();
        System.out.println("first = " + first);
        Integer last = treeSet.last();
        System.out.println("last = " + last);
        Integer lower = treeSet.lower(4);
        System.out.println("lower = " + lower);
        Integer higher = treeSet.higher(4);
        System.out.println("higher = " + higher);

        Iterator<Integer> iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        SortedSet<Integer> subSet = treeSet.subSet(2, 6);
        for (Integer integer : subSet) {
            System.out.println("integer = " + integer);
        }

        SortedSet<Integer> headSet = treeSet.headSet(4);
        for (Integer head : headSet) {
            System.out.println("head = " + head);
        }
        SortedSet<Integer> tailSet = treeSet.tailSet(4);
        for (Integer tail : tailSet) {
            System.out.println("tail = " + tail);
        }


    }
}
